﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Text;
using Newtonsoft.Json;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    public class HRWatchScheduleController : AdminBaseController
    {
        //
        // GET: /Admin/HRWatchSchedule/

        [AuthorityActionFilter("m_HRWatchSchedule", Order = 1)]
        [LogPageActionFilter("值班安排", Order = 2)]
        public ActionResult Index()
        {
            return HRWatchSchedule();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="showDate">显示时间</param>
        /// <param name="method">提交方式 0正常1往前一个月2往后一个月</param>
        /// <returns></returns>
        public ActionResult HRWatchSchedule(string showDate=null,string method="0")
        {

            BLL.HRWatchSchedule watchScheduleBLL = new BLL.HRWatchSchedule();

            DateTime dtShow = new DateTime(DateTime.Now.Year,DateTime.Now.Month,1);
            if (showDate != null && showDate != "")
            {
                showDate = showDate + "01日";
                dtShow = DateTime.Parse(showDate);
            }
            switch (method)
            {
                case "1":
                    dtShow= dtShow.AddMonths(-1);
                    break;
                case "2":
                    dtShow = dtShow.AddMonths(1);
                    break;
                default:
                    break;
            }

            ViewData["currentDate"] = dtShow.ToString("yyyy年MM月");

            int monthDay = DateTime.DaysInMonth(dtShow.Year, dtShow.Month);//当前月份有几天
            int week = int.Parse(dtShow.DayOfWeek.ToString("d"));//当前月份第一天是星期几

            //获取值班数据
            string condition = "a.FEntityNo='" + Common.GlobalConstant.ENTITYNO + "' and Year(a.FWorkDate)=" + dtShow.Year + " and Month(a.FWorkDate)=" + dtShow.Month + " order by a.FWorkDate,a.FSeq";
            List<Model.HRWatchSchedule> watchScheduleList= watchScheduleBLL.GetModelList(condition);

            StringBuilder str = new StringBuilder();
            int day = 1;
            str.Append("<table border='1px' cellpadding='0' cellspacing='0' class='tb1'>");

            for (int i = 0; i < 7; i++)
            {
                if (i == 0)
                {
                    str.Append("<tr style='height:20px;'>");
                    str.Append("<td>日</td>");
                    str.Append("<td>一</td>");
                    str.Append("<td>二</td>");
                    str.Append("<td>三</td>");
                    str.Append("<td>四</td>");
                    str.Append("<td>五</td>");
                    str.Append("<td>六</td>");
                    str.Append("</tr>");
                }
                else
                {
                    str.Append("<tr>");
                    for (int j = 0; j < 7; j++)
                    {

                        if (week > 0)
                        {
                            str.Append("<td>&nbsp;</td>");
                        }
                        else
                        {
                            if (day <= monthDay)
                            {
                                int y = 1;
                                str.Append("<td><div style='height:90px;'  id='" + day + "'><b>" + day + "</b></br>");
                                str.Append("<table>");

                                for (int z = 0; z < watchScheduleList.Count; z++)
                                {
                                    if (watchScheduleList[z].FWorkDate.Day == day)
                                    {
                                        str.Append("<tr style='height:10px'>");
                                        str.Append("<td style='text-align:right;'>值班领导：</td>");
                                        str.Append("<td style=' text-align:left;'>" + watchScheduleList[z].FEmpName + "</td>");
                                        str.Append("</tr>");
                                        str.Append("<tr style='height:10px'>");
                                        str.Append("<td style='text-align:right;'>值班科室：</td>");
                                        str.Append("<td style=' text-align:left;'>" + watchScheduleList[z].FDeptName + "</td>");
                                        str.Append("</tr>");
                                    }
                                }

                                str.Append("</table>");
                                str.Append("</div></td>");


                                day++;
                            }
                            else
                            {
                                str.Append("<td>&nbsp;</td>");
                            }
                        }
                        week--;

                    }
                    str.Append("</tr>");
                }
            }
            str.Append("</table>");


            ViewData["scheduleTable"] = str.ToString();
            return View("Index");
        }

        /// <summary>
        /// 前/后一个月
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult BtnPrevNext()
        {
            string showDate = Fetch.Post("hdCurrentDate");
            string method = Fetch.Post("hdMethod");
            return HRWatchSchedule(showDate, method);
        }

        /// <summary>
        /// 增加值班安排
        /// </summary>
        /// <returns></returns>
        public ActionResult HRWatchScheduleAdd()
        {
            //获取部门
            BLL.HRDept deptBLL = new BLL.HRDept();
            List<Model.HRDept> deptList = deptBLL.getDeptListNoVirtual(GlobalConstant.ENTITYNO);


            ViewData["Dept"] = deptList;

            BLL.HREmpl emplBLL = new BLL.HREmpl();
            List<Model.HREmpl> emplList = emplBLL.GetEmplList(GlobalConstant.ENTITYNO);
            ViewData["Empl"] = emplList;

            return View();
        }

        /// <summary>
        /// 保存值班安排数据
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public string Save(string showDate,string data)
        {
            string message = "";
            try
            {
                Model.HRWatchSchedule HRWatchScheduleModel = JsonConvert.DeserializeObject<List<Model.HRWatchSchedule>>(data)[0];

                BLL.HRWatchSchedule HRWatchScheduleBLL = new BLL.HRWatchSchedule();

                HRWatchScheduleModel.FEntityNo = GlobalConstant.ENTITYNO;
                HRWatchScheduleModel.FWorkDate = DateTime.Parse(showDate);
                HRWatchScheduleModel.FSeq = "0";

                HRWatchScheduleBLL.AddSchedule(HRWatchScheduleModel);
                message = "success";
            }
            catch (Exception ex)
            {
                message = "值班安排增加失败，原因为："+ex.ToString();
            }

            return message;
        }

    }
}
